<?php
/**
 * 登录态
 * */
require_once(LIB_PATH.'/request/myrequest.php'); 
 
class mylogin{
    public static $isLogined = false;
    public static $uid = 0;

    public static function createOrGetRandId(){
        return self::getOrCreateRandId();
    }

    public static function getOrCreateRandId(){
        $randId = self::getRandId();
        if(!$randId){
            $randId = self::createRandId();//已经不读数据库，没有垃圾数据
        }
        return $randId;
    }
    
    public static function getRandId(){
        $cookieRandId = c('QI_RID');//已经登录
        $goodKey = md5(COOKIE_KEY1.$cookieRandId .COOKIE_KEY2);

        if(!$_SERVER['PUBLIC']) return $cookieRandId;//未上线的时候，自动登录

        if(c('QI_KEY')==$goodKey){
            return  $cookieRandId;
        }
        return '';
    }

    //对比数据库版本，性能提升31倍
    public static function createRandId(){
        $randU = uniqid(md5(microtime(true)),true);//php7.1使用：session_create_id()
        $identify = self::getFiveKey($randU);
        $randId = $identify.$randU;

        $expire =time()+3600*24*365;
        $mainDomain = myrequest::getMainDomain();
        setCookie("QI_RID",$randId,$expire,"/",$mainDomain);
        setCookie("QI_KEY",md5(COOKIE_KEY1.$randId.COOKIE_KEY2),$expire,"/",$mainDomain);
        return $randId;
    }

    public static function getFiveKey($uid){
        return substr(md5($uid.COOKIE_KEY1),10,5);
    }

    public static function isRandIdGood($randId){
        $uid = substr($randId,5);
        $identify = substr($randId,0,5);
        if($identify == self::getFiveKey($uid)) return true;
        return false;
    }
	
	/**
	 * 获取登录用户UID
	 * @param bool $needLogin
	 * @return COOKIE值|int
	 */
    public static function getUid($needLogin=false){
        if(self::$uid) return self::$uid;

        //to support no cookie in wechat
        $uid = r_int('loginUid');
        $time = r('loginTime');
        if($time < (time()- LOGIN_EXPIRE)) return 0;//登录有效期

        $expectKey = md5($time.COOKIE_KEY1.$uid.COOKIE_KEY2);
        //判断正确不
        if($expectKey == r('loginKey')){
            $user = new User();
            $user->find($uid);
            if(!$user->openId){//没有授权登录
                return 0;
            }

            return $uid;
        }

        //cookie login
        $uid = c('QI_UID');
        $time = c('QI_TIME');
        $expireKey = md5($time.COOKIE_KEY1.$uid.COOKIE_KEY2);
        if($expireKey == c('QI_KEY')){
            return $uid;
        }

        if($needLogin){
            die(json_encode(['code'=>-100,'info'=>'请先登录']));
        }
        return 0;
    }

    public static function getLoginInfo(){
        //to support no cookie in wechat
        $uid = r('loginUid');
        $time = r('loginTime');
        $expectKey = md5($time.COOKIE_KEY1.$uid.COOKIE_KEY2);
        if($expectKey == r('loginKey')){
            $loginInfo['loginUid']  = $uid ;
            $loginInfo['loginTime'] = $time ;
            $loginInfo['loginKey']   = $expectKey;
            return $loginInfo;
        }

        //cookie login
        $uid = c('QI_UID');
        $time = c('QI_TIME');
        $expectKey = md5($time.COOKIE_KEY1.$uid.COOKIE_KEY2);
        if($expectKey == c('QI_KEY')){
            $loginInfo['loginUid']  = $uid ;
            $loginInfo['loginTime'] = $time ;
            $loginInfo['loginKey']   = $expectKey;
            return $loginInfo;
        }
        return false;
    }
	
	/**
	 * 判断用户是否登录
	 * @param string $uid
	 * @return bool
	 */
    public static function isLogin($uid=''){
        if(self::getUid()){
            return true;
        }

        return false;
    }

    public static function isAdmin(){
        if(!isset($_COOKIE['u_c_'])) return false;
        if(!isset($_COOKIE['u_c_key'])) return false;

        preg_match("/(\d+)ttt__(.*)/i",$_COOKIE['u_c_'],$matches);
        $uid = $matches[1];
        if(mylogin::getMd5Pwd($uid) == $_COOKIE['u_c_key']){
            return true;
        }

        return false;
    }

    public static function getMd5Pwd($pwd){
        return md5(COOKIE_KEY1 . $pwd . COOKIE_KEY2);
    }

    public static function login($uid){
        self::$isLogined = true;
        self::$uid = $uid;

        $time = time();
        $expire = $time+3600*24*365;
        setCookie("QI_UID",$uid,$expire,"/",COOKIE_DOMAIN);
		$_COOKIE['QI_UID'] = $uid;
        $loginInfo['loginUid'] = $uid;
		
        setCookie("QI_TIME",$time,$expire,"/",COOKIE_DOMAIN);
		$_COOKIE['QI_TIME'] = $time;
        $loginInfo['loginTime'] = $time;
		
		$key = md5($time.COOKIE_KEY1.$uid.COOKIE_KEY2);
        setCookie("QI_KEY",$key,$expire,"/",COOKIE_DOMAIN);
		$_COOKIE['QI_KEY'] = $key;
        $loginInfo['loginKey'] = $key;

		return $loginInfo;
    }

	public static function logout(){
        self::$isLogined = false;
        self::$uid = 0;

		foreach($_COOKIE as $key=>$value){
			if(!($key == 'QI_VIEW_ID' || $key=='QI_SHOW_PAGE')){	//只是清理自己，不清理好友关系
				setCookie($key,"",time()-3600*24*365);
				setCookie($key,"",time()-3600*24*365,COOKIE_DIR);
				setCookie($key,"",time()-3600*24*365,"/");
				setCookie($key,"",time()-3600*24*365,"/",'.'.myrequest::getMainDomain());//由于默认只删除当前子域名，不删除顶级域名
			}
		}
	}
    
	public static function logoutAll(){
		foreach($_COOKIE as $key=>$value){
				setCookie($key,"",time()-3600*24*365,COOKIE_DIR);
				setCookie($key,"",time()-3600*24*365,"/");
				setCookie($key,"",time()-3600*24*365,"/",'.'.myrequest::getMainDomain());//由于默认只删除当前子域名，不删除顶级域名
		}
	}
 }   
